<?php
namespace Wechat\Controller;

use Wechat\Controller\AppController;
use Cake\Routing\Router;
use Psr\Log\LogLevel;
use Cake\ORM\TableRegistry;

/**
 * 需要微信自动登录的单页面
 */
class PagesController extends AppController
{

    var $allowedActions = [
        'yiqizhuan',
        'sjyiqizhuan'
    ];

    var $wxLoginActions = [
        'dianshang',
        'baoma',
        'yiqizhuan',
        'sjyiqizhuan'
    ];
    /**
     * 线上电商认领
     * @link https://www.mofing.com/wechat/pages/dianshang.html
     */
    public function dianshang(){
        //获取用户token
        $this->redirect("/web/console/index.html?uid=".$this->uid."&token=".$this->getTokenByUid()."#/home");
    }
    /**
     * 宝码单独跳转地址
     * @link http://www.mofing.com/wechat/pages/baoma.html
     */
    public function baoma(){
        $this->redirect("/web/baobao/index.html#/baoma");
    }
    /**
     * 查看邀请奖励
     *
     * @link http://www.mofing.com/wechat/pages/yiqizhuan/439124.html
     */
    public function yiqizhuan($fuid = 0)
    {
        $wxcookieMember = $this->Cookie->read($this->wxloginCookiesKey);
        $title = "阿宝一起赚";
        $imgUrl = "http://cdn6.mofing.com/static/img/wx/zzz.jpg";
        $desc = "阿宝撒现金，大家一起赚";
        if (empty($fuid)) {
            // 获取cookie中的id
            $fuid = $this->fuid;
        }
        $url = Router::url([
            "_ext" => "html",
            ! empty($this->uid) ? $this->uid : $fuid
        ], true);
        if ($this->request->is("json")) {
            $this->set("title", $title);
            $this->set("imgUrl", $imgUrl);
            $this->set("desc", $desc);
            $this->set("code", 200);
            $this->set("login", ! empty($wxcookieMember));
            $this->set("uid", $this->uid);
            $this->set("shareUrl", $url);
        } else {
            // 只有自己没有注册木丰号之前，保存邀请者id，否则邀请者无意义
            if (empty($wxcookieMember["id"]) || ! empty($fuid)) {
                $this->Cookie->configKey("invite", "encryption", false);
                $this->Cookie->write("invite", $fuid);
                $this->log("邀请奖励:" . $this->request->here, LogLevel::INFO);
            }
            $data = [
                "title" => $title,
                "img" => $imgUrl,
                "url" => $url,
                "desc" => $desc
            ];
            // 跳转静态页面
            // $this->redirect("/web/sabi/index.html#/");
            $this->viewBuilder()->setLayout("");
            $id = 10000;
            $this->loadComponent('Wechat.Wechat', [
                'id' => $id
            ]);
            // $prox = $this->request->is("ssl") ? "https" : "http";
            // header('Access-Control-Allow-Origin: ' . $prox . '://www.mofing.com');
            $signPackage = $this->Wechat->GetSignPackage();
            // unset($signPackage["rawString"]);
            $this->set("sign", $signPackage);
            $this->set("data", $data);
            $this->set("uid", $this->uid);
        }
    }

    /**
     * 商家一起赚
     *
     * @param number $fuid            
     * @link http://www.mofing.com/wechat/pages/sjyiqizhuan/439124.html
     */
    public function sjyiqizhuan($fuid = 0)
    {
        $wxcookieMember = $this->Cookie->read($this->wxloginCookiesKey);
        $title = "阿宝一起赚";
        $imgUrl = "http://cdn6.mofing.com/static/img/wx/zzz.jpg";
        $desc = "阿宝撒现金，大家一起赚";
        if (empty($fuid)) {
            // 获取cookie中的id
            $fuid = $this->fuid;
        }
        $url = Router::url([
            "_ext" => "html",
            ! empty($this->uid) ? $this->uid : $fuid
        ], true);
        $this->viewBuilder()->setLayout("");
        $data = [
            "title" => $title,
            "img" => $imgUrl,
            "url" => $url,
            "desc" => $desc
        ];
        $id = 10000;
        $this->loadComponent('Wechat.Wechat', [
            'id' => $id
        ]);
        $signPackage = $this->Wechat->GetSignPackage();
        $this->set("sign", $signPackage);
        $this->set("data", $data);
        $this->set("uid", $this->uid);
    }
    private function getTokenByUid(){
        $Users = TableRegistry::get ( "Users" );
        $uinfo = $Users->find()->where(["userId"=>$this->uid])->select(["token"])->first();
        if($uinfo){
            return $uinfo->token;
        }
        return "";
    }
}
